package com.woniuxy.intellihome.controller.admin;


import com.woniuxy.intellihome.entity.Admin;
import com.woniuxy.intellihome.entity.Category;
import com.woniuxy.intellihome.entity.ResponseEntity;
import com.woniuxy.intellihome.entity.VAdmin;
import com.woniuxy.intellihome.mapper.AdminPermMapper;
import com.woniuxy.intellihome.service.AdminService;
import com.woniuxy.intellihome.service.VAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-08-11
 */
@CrossOrigin(value = "http://localhost:8081",allowCredentials = "true")
@RestController
@RequestMapping("admin/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private VAdminService vadminService;
    @Autowired
    private AdminPermMapper adminPermMapper;

    /**
     * 通过账号判断该账号是否存在再判断密码是否正确
     * 真确就登录成功否则登录失败。。。.
     */
    @PostMapping("login")
    public ResponseEntity login(@RequestBody Admin admin, HttpSession session){
        ResponseEntity responseEntity = new ResponseEntity();
        System.out.println("传过来的admin======"+admin);
        Admin admin1 = adminService.getByAccount(admin.getAccount());
        System.out.println(admin1);
        if (admin1!=null){
            if (admin1.getStatus().equals("y")){
            System.out.println("管理员"+admin1+"存在!");
            //判断是否是admin超级管理员（超级管理员有增加删除普通管理员的操作）
            if (admin1.getAccount().equals("admin")){
                //是超级管理员
                if(admin.getPassword().equals(admin1.getPassword())){
                    session.setAttribute("admin",admin);
                    System.out.println("超级管理员登录");
                    responseEntity.setCode(200);
                    responseEntity.setMsg("登录成功！");
                    responseEntity.setData(admin);
                    return responseEntity;
                }else {
                    return new ResponseEntity(400,"密码不正确");
                }
            }else {
                //普通管理员
                //判断密码是否正确
                if(admin.getPassword().equals(admin1.getPassword())){
                    session.setAttribute("admin1",admin1);
                    System.out.println("普通管理员登录");
                    responseEntity.setCode(200);
                    responseEntity.setMsg("登录成功！");
                    responseEntity.setData(admin1);
                    return responseEntity;
                }else {
                    return new ResponseEntity(400,"密码不正确");
                }
            }
        }else {
                System.out.println("管理员"+admin1+"不存在!！！");
                return new ResponseEntity(401,"用户名不存在！");
        }
        }else {
            System.out.println("管理员"+admin1+"不存在!！！");
            return new ResponseEntity(401,"用户名不存在！");
        }
    }

    /**
     *获取管理员列表
     */
    @PostMapping("list")
    public ResponseEntity<List<VAdmin>> vadminList(){
        List<VAdmin> vadmins = vadminService.getAll();
        return new ResponseEntity<>(vadmins);
    }

    /**
     * 通过管理员id进行软删除（修改状态为n）
     */
    @GetMapping("del/{id}")
    public ResponseEntity del(@PathVariable int id){
        String status="n";
        System.out.println("id======="+id);
        /*adminService.delById(status,id);*/
        adminPermMapper.delByAdminId(id);
        adminService.delById(id);
        return ResponseEntity.SUCCESS;
    }

    /**
     * 超级管理员可以增加普通管理员
     */
    @PostMapping("add")
    public ResponseEntity add(@RequestBody Admin admin){
        System.out.println(admin);
        Admin admin1 = adminService.getByAccount(admin.getAccount());
        System.out.println(admin1);
        if(admin1!=null){
            if(admin1.getStatus().equals("y")){
                //该账号已存在增加失败
                System.out.println("该账号已存在，增加失败");
                return new ResponseEntity(400,"该账号已存在，增加失败");
            }else {
                adminService.addAdmin(admin.getAccount(),admin.getPassword(),"y");
                Admin admin2=adminService.getByAccountAndStatus(admin.getAccount(),"y");
                System.out.println(admin2);
                adminPermMapper.addAdminPerm(admin2.getId(),2);//表示普通管理员！
                //该账号不存在，增加成功
                return ResponseEntity.SUCCESS;
            }
        }else {
            adminService.addAdmin(admin.getAccount(),admin.getPassword(),"y");
            Admin admin2=adminService.getByAccountAndStatus(admin.getAccount(),"y");
            System.out.println(admin2);
            adminPermMapper.addAdminPerm(admin2.getId(),2);//表示普通管理员！
            //该账号不存在，增加成功
            return ResponseEntity.SUCCESS;
        }

    }

}

